﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Security.Cryptography;
using System.Data;

namespace 财会系.Admin
{
    public partial class AdminAdd : System.Web.UI.Page
    {
        BllAdmin b = new BllAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Type"] != null)
                {
                    Grade_Name();
                }
            }       
        }

        public void Grade_Name()
        {
            BllBackground bbg = new BllBackground();
            DataTable dt = bbg.Nav_Name_All();

            foreach(DataRow my in dt.Rows)
            {
                if(my["Id"].ToString()=="2")
                {
                    Grade_Name1.InnerHtml = my["Name"].ToString();
                    Check_Geade1.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "3")
                {
                    Grade_Name2.InnerHtml = my["Name"].ToString();
                    Check_Geade2.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "4")
                {
                    Grade_Name3.InnerHtml = my["Name"].ToString();
                    Check_Geade3.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "5")
                {
                    Grade_Name4.InnerHtml = my["Name"].ToString();
                    Check_Geade4.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "6")
                {
                    Grade_Name5.InnerHtml = my["Name"].ToString();
                    Check_Geade5.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "7")
                {
                    Grade_Name6.InnerHtml = my["Name"].ToString();
                    Check_Geade6.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "8")
                {
                    Grade_Name7.InnerHtml = my["Name"].ToString();
                    Check_Geade7.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "9")
                {
                    Grade_Name8.InnerHtml = my["Name"].ToString();
                    Check_Geade8.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "10")
                {
                    Grade_Name9.InnerHtml = my["Name"].ToString();
                    Check_Geade9.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "11")
                {
                    Grade_Name10.InnerHtml = my["Name"].ToString();
                    Check_Geade10.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "12")
                {
                    Grade_Name11.InnerHtml = my["Name"].ToString();
                    Check_Geade11.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "13")
                {
                    Grade_Name12.InnerHtml = my["Name"].ToString();
                    Check_Geade12.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "14")
                {
                    Grade_Name13.InnerHtml = my["Name"].ToString();
                    Check_Geade13.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "15")
                {
                    Grade_Name14.InnerHtml = my["Name"].ToString();
                    Check_Geade14.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "17")
                {
                    Grade_Name15.InnerHtml = my["Name"].ToString();
                    Check_Geade15.Value = my["Id"].ToString();
                }
            }

            
        }

        protected void btnAdminAdd_Click(object sender, EventArgs e)
        {
            //添加管理员
            ModelAdmin m = new ModelAdmin();
            m.Acc = txtAcc.Text.Trim();
            m.Pwd = txtPwd.Text.Trim();
            m.Name = txtName.Text.Trim();

            if(Grade_Super.Checked==true)
            {
                m.Grade = "1";
                m.Type = 0;
            }
            if(Grade_Teach.Checked==true)
            {
                m.Grade = Grade_Num();
                m.Type = 0;
            }
            if(Grade_Student.Checked==true)
            {
                m.Grade = Grade_Num();
                m.Type = 1;
            }


            string strword = b.Add(m);
            if (strword == "添加成功!")
            {
                Response.Write("<script>confirm('" + strword + "')?window.location='AdminAdd.aspx':window.location='AdminAdd.aspx';</script>");
            }
            else
            {
                Response.Write("<script>confirm('" + strword + "')?window.location='AdminAdd.aspx':window.location='AdminAdd.aspx';</script>");                
            }
        }

        private string Grade_Num()
        {
            string strGrade = "";

            if (Check_Geade1.Checked)
                strGrade += Check_Geade1.Value+",";
            if (Check_Geade2.Checked)
                strGrade += Check_Geade2.Value + ",";
            if (Check_Geade3.Checked)
                strGrade += Check_Geade3.Value + ",";
            if (Check_Geade4.Checked)
                strGrade += Check_Geade4.Value + ",";
            if (Check_Geade5.Checked)
                strGrade += Check_Geade5.Value + ",";
            if (Check_Geade6.Checked)
                strGrade += Check_Geade6.Value + ",";
            if (Check_Geade7.Checked)
                strGrade += Check_Geade7.Value + ",";
            if (Check_Geade8.Checked)
                strGrade += Check_Geade8.Value + ",";
            if (Check_Geade9.Checked)
                strGrade += Check_Geade9.Value + ",";
            if (Check_Geade10.Checked)
                strGrade += Check_Geade10.Value + ",";
            if (Check_Geade11.Checked)
                strGrade += Check_Geade11.Value + ",";
            if (Check_Geade12.Checked)
                strGrade += Check_Geade12.Value + ",";
            if (Check_Geade13.Checked)
                strGrade += Check_Geade13.Value + ",";
            if (Check_Geade14.Checked)
                strGrade += Check_Geade14.Value + ",";
            if (Check_Geade15.Checked)
                strGrade += Check_Geade15.Value;

            return strGrade;
        }
    }
}